package edu.jit.flowers.controller;


import java.io.File;
import java.util.List;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import edu.jit.flowers.bean.Carts;
import edu.jit.flowers.bean.Flowers;
import edu.jit.flowers.bean.Users;
import edu.jit.flowers.service.CartsService;
import edu.jit.flowers.service.FlowersService;
import edu.jit.flowers.service.UsersService;
import edu.jit.flowers.utils.BaseServlet;

public class FlowersServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private FlowersService fService = new FlowersService();
	private CartsService cService = new CartsService();
	private UsersService uService = new UsersService();
	//后台
	public String getAllFlowers(HttpServletRequest request, HttpServletResponse response) {
		List<Flowers> flowers = fService.getAllFlowers();
		request.setAttribute("flowers", flowers);
		return "forward:backJsp/flowerShow.jsp";
		// flowerShow.jsp
	}
	//前台
	public String getFlowers(HttpServletRequest request, HttpServletResponse response) {
		List<Flowers> flowers = fService.getAllFlowers();
		request.setAttribute("flowers", flowers);
		return "forward:flowerList.jsp";
		// flowerShow.jsp
	}
	public String deleteFlowersByFName(HttpServletRequest request, HttpServletResponse response) {
		String fName = request.getParameter("fName");
		fService.deleteFlowers(fName);
		return "redirect:Flowers?method=getAllFlowers";
	}
	//添加花
	public String addFlowers(HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (!ServletFileUpload.isMultipartContent(request)) {
			throw new UnavailableException("请检查form表单的enctype是否为multipart/form-data");
		}
		DiskFileItemFactory dfactory = new DiskFileItemFactory();
		dfactory.setSizeThreshold(4 * 1024);
		ServletFileUpload sfupload = new ServletFileUpload(dfactory);
		List<FileItem> items = sfupload.parseRequest(request);
		String fName = null;
		int fPrice = 0;
		String fCategory = null;
		String fDesc = null;
		String fPic = null;
		String fOnSale = null;
		for (FileItem item : items) {
			if (item.isFormField()) {
				switch (item.getFieldName()) {
				case "fName":
					fName = item.getString("utf-8");
					break;
				case "fPrice":
					fPrice = Integer.parseInt(item.getString().trim());
					break;
				case "fCategory":
					fCategory = item.getString("utf-8");
					break;
				case "fDesc":
					fDesc = item.getString("utf-8");
					break;
				default:
					fOnSale = item.getString("utf-8");
				}
				//System.out.println(item.getString("utf-8"));
			} else {// 如果是个文件，直接存到硬盘中
				String fileName = item.getName();
				String path = getServletContext().getRealPath("upload");
				String url = getServletContext().getContextPath() + File.separator + "upload" + File.separator
						+ fileName;
				fPic = url;
				File f = new File(path, fileName);
				//System.out.println(f.getAbsolutePath());
				item.write(f);
				item.delete();
			}
		}
		fService.addFlowers(new Flowers(0, fName, fPrice, fCategory, fDesc, fPic, fOnSale));
		return "redirect:Flowers?method=getAllFlowers";
	}
	//添加到购物车
	 public void addToCart(HttpServletRequest request, HttpServletResponse response) {
	    	String fName=request.getParameter("fName");
			Flowers f = fService.getAllFlowersByfName(fName);
			String username = (String) request.getSession().getAttribute("username");
			Users u = uService.getAllCommonsByUName(username);
			cService.addCarts(new Carts(0,u.getuId(),f.getfId(),1,1.0,f.getfPrice(),f.getfName(),f.getfPrice()));
			//return "redirect:showCartItems.jsp";
		}

}
